<?php
/**
 * 通用异常抛出类
 * User: 13651
 * Time: 2025/9/5 11:51
 */

namespace App\Exceptions;

use Exception;
use Throwable;

class BusinessException extends  Exception
{
    private $errno;
    private $errmsg;
    public function __construct(array $responseCode, array $extend = [], Throwable $previous = null)
    {
        list($code, $message) = $responseCode;
        $this->errno =$code;
        $this->errmsg = $message;
        // 附加的信息
        if(!empty($extend)) {
            $field = array_keys($extend)[0];
            $this->errmsg = $extend[$field];
        } else {
            $this->errmsg = preg_replace("/%[a-z]+/", '', $this->errmsg);
        }
        parent::__construct($this->errmsg, $code, $previous);
    }

    public function render()
    {
        /**
         * 错误返回格式
         */
        $response = [
            'return_code' => $this->errno,
            'return_msg' => $this->errmsg
        ];
        return response()->json($response);
    }
}